<?php
class Branch_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function add($branch_name, $branch_location, $branch_tel, $active){
		$data = array(
				'branch_tel' => utf8_encode($branch_tel),
				'branch_name' => utf8_encode($branch_name),
				'branch_location' => utf8_encode($branch_location),
				'branch_active' => $active
		);
		if ($this->db->insert('branch', $data))
			return $this->db->insert_id();
		else
			return -1;
	}

	public function update($branch_id, $branch_name, $branch_location, $branch_tel, $active){
		$data = array(
				'branch_tel' => utf8_encode($branch_tel),
				'branch_name' => utf8_encode($branch_name),
				'branch_location' => utf8_encode($branch_location),
				'branch_active' => $active
		);
		$this->db->where('branch_id', $branch_id);
		if ($this->db->update('branch', $data))
			return true;
		else
			return false;
	}

	public function get_id($branch_name){
		$query = $this->db->where('LOWER(branch_name)', strtolower(utf8_encode($branch_name)))->get('branch');
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return $row->branch_id;
		}else
			return -1;
	}
	
	public function check_duplication_location($branch_location){
		$query = $this->db->where('LOWER(branch_location)', strtolower(utf8_encode($branch_location)))->get('branch');
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return true;
		}else
			return false;
	}
	
	public function check_duplication($branch_location, $id){
		$this->db->where('LOWER(branch_location)', strtolower(utf8_encode($branch_location)));
		$this->db->where('branch_id !=', $id);
		$query = $this->db->get('branch');
		if ($query->num_rows() > 0)
		{
			$row = $query->row();
			return true;
		}else
			return false;
	}

	public function get_branch_by_id($id){
		$query = $this->db->where('branch_id', $id)->get('branch');
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}else
			return null;
	}
	
	public function get_branch_by_code($code){
		$query = $this->db->where('branch_code', $code)->get('branch');
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}else
			return null;
	}

	public function search($string, $limit, $start){
		//if (strlen($string)>0){
			$this->db->like('branch_name', $string, 'after');
			$this->db->or_like('branch_name', ' '.$string);
			$this->db->or_like('branch_location', $string, 'after');
			$this->db->or_like('branch_location', ' '.$string);
			$this->db->order_by("branch_name", "asc");
			$this->db->order_by("branch_location", "asc");
			$this->db->limit($limit, $start);
			$query = $this->db->get('branch');
			if ($query->num_rows() > 0)
			{
				return $query->result();
			}
		//}
		return null;
	}
	
	public function count_search($string){
		//if (strlen($string)>0){
		$this->db->like('branch_name', $string, 'after');
		$this->db->or_like('branch_name', ' '.$string);
		$this->db->or_like('branch_location', $string, 'after');
		$this->db->or_like('branch_location', ' '.$string);
		$this->db->from('branch');
		return $this->db->count_all_results();

	}
}